class Api::V1::UsersController < Api::V1::BaseController

	skip_before_filter :signed_in_user, :only => [:create]

	skip_before_filter :verify_authenticity_token, :only => [:create]
	before_action :set_user, only: [:show, :edit, :update, :destroy]


  def index
    @users = User.all
  end

	def show
	end

	def new
		@user = User.new
	end

	def create
		@user = User.new(user_params)
		if @user.save
	    	sign_in @user
       		render :status => 201, 
              		:json => {  
                      	:status_code => 201,
                      	:message => "You have signed up successfully!",
                      	:content => session[:remember_token] 
					}
		else
			errors = ""
			@user.errors.full_messages.each { |error|  errors += error + ","}
	     	render :status => 201, 
              		:json => { 
                    	:status_code => 401,
                      	:message => "Error signing up!", 
                      	:content => errors
					}
		end
	end

	def update
		if @user.update(user_params)
			render :status => 200,
					:json => { 
						:status_code => 200,
						:message => "Resource updated succesfully!",
						:content => ""
					}
		else
			render :status => 406, 
					:json => {
						:status_code => 406,
						:message => "Error updating resource!",
						:content => @user.errors.full_messages
					}
		end
	end

	def destroy
		if @user.destroy
			render :status => 200,
					:json => { 
						:status_code => 200,
						:message => "Resource deleted succesfully!",
						:content => ""
					}
		else
			render :status => 406, 
					:json => {
						:status_code => 406,
						:message => "Error deleting resource!",
						:content => @user.errors.full_messages
					}
		end
	end

	private

		def set_user
     		@user = User.find(params[:id])
     		rescue ActiveRecord::RecordNotFound
				error = "The resource that you were looking for could not be found."
				render :status => 404, 
						:json => { 
							:status_code => 404,
							:message => error,
							:content => ""
						 }
   		end

	    def user_params
	      params.require(:user).permit(:name, :email, :password,
	                                   :password_confirmation)
	    end
end
